<?php
// 订单备注，或其他情况备注等。
class MessageAction extends CommonAction {
    
	public function _filter(&$map){
		 if( $_REQUEST['content'] != '' )
             $map['content'] = trim($_REQUEST['content']);
         if( $_REQUEST['reply'] != '' )
             $map['reply'] = trim($_REQUEST['reply']);
         if( $_REQUEST['mall_no'] != '' )
             $map['mall_no'] = trim($_REQUEST['mall_no']);
         if( $_REQUEST['depart'] != '' )
             $map['depart'] = trim($_REQUEST['depart']);
         if( $_REQUEST['status'] != '' )
             $map['status'] = trim($_REQUEST['status']);
	}

    public function _before_insert()
     {
         //判断是不是异常订单，是否存在这个订单
        $rs = M('Sale')->where('mall_no like "%'.trim($_POST['mall_no']).'%" ' )->field('id,prepare_status,order_status')->find();
        if($rs)
            $_POST['obj_id'] = $rs['id'];

        //已经发货或交易成功，或交易关闭的，则视为异常订单
        //如果已经打印配货了，则置为异常订单
        if($rs['prepare_status'] > 0 || $rs['order_status'] > 2)  
            $_POST['exception_order'] = 1 ; 
    }


  public function _before_index(){
    $message = M('Message');
    $count_todo = $message->where('status = 0')->count('id');
    $count_doing = $message->where('status = 1')->count('id');
    $count_exception = $message->where('exception_order = 1')->count('id');
    $this->assign('count_todo',$count_todo);
    $this->assign('count_doing',$count_doing);
    $this->assign('count_exception',$count_exception);
  }  
	
  public function reply(){
     $this->edit('Message','reply');     
  }

/**
 *创建备注
 *$obj_id 备注对象,默认为sale_id
 *$depart 1客服,2仓储
 *$content 备注内容
 *$creater_id 创建人
 *$exception_order 是否是异常订单,默认为异常订单
 */
 public function write_message($obj_id,$depart,$content,$creater_id,$exception_order=1,$obj_type = "Sale"){
     $data = array(
         'obj_type' => $obj_type,
         'obj_type_id' => $obj_type_id,
         'depart' => $depart,
         'content' => $content,
         'creater_id' => $creater_id,
         'exception_order' => $exception_order,
         'create_time' => time()
     );
     if($obj_type == "Sale"){
        $data['obj_type_id'] = 1;
        $mall_no = M('Sale')->where('id = '.$obj_id)->getField('mall_no');
        $data['mall_no'] = $mall_no;
     } 

     M('Message')->data($data)->add();
 }  
    
}
?>
